package cn.mumzone.pos.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.webkit.WebView;
import android.widget.LinearLayout;

import java.util.List;
import java.util.Map;

import butterknife.BindView;
import cn.mumzone.basic.base.BaseActivity;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.HelpCenterChildEntity;
import cn.mumzone.pos.presenter.HelpCenterPresenter;
import cn.mumzone.pos.presenter.contract.HelpCenterContract;
import cn.mumzone.pos.ui.adapter.ADA_HelpCenter;
import cn.mumzone.pos.ui.widgets.AnimatedExpandableListView;

/**
 * Created by JoeLjt on 2019/11/18.
 * Email: lijiateng1219@gmail.com
 * Description:
 */

public class ACT_HelpCenter extends BaseActivity<HelpCenterPresenter> implements HelpCenterContract.View {

    @BindView(R.id.title_list)
    AnimatedExpandableListView titleList;
    @BindView(R.id.ll_content)
    LinearLayout llContent;

    private WebView mWebView;

    private ADA_HelpCenter mAdapter;

    @Override
    protected void initEventAndData() {

        initView();

        mPresenter.getHelpCenterTitle();

        //        mHandler.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                mPresenter.getHelpCenterTitle();
//            }
//        }, 1500);
    }

    private void initView() {

        mWebView = new WebView(this);
        mWebView.setBackgroundColor(Color.parseColor("#F0F0F0"));
        mWebView.setHorizontalScrollBarEnabled(false);
        mWebView.setVerticalScrollBarEnabled(false);
        llContent.addView(mWebView);

        mAdapter = new ADA_HelpCenter(this, titleList);
        titleList.setAdapter(mAdapter);
        mAdapter.setOnSelectedListener(new ADA_HelpCenter.OnSelectedListener() {
            @Override
            public void onChildSelected(String content) {
                mWebView.loadDataWithBaseURL(null,
                        content, "text/html", "UTF-8", null);
            }
        });

    }

    @Override
    protected int getLayout() {
        return R.layout.act_help_center;
    }

    public static void startAct(Context context) {
        Intent intent = new Intent(context, ACT_HelpCenter.class);
        context.startActivity(intent);
    }

    @Override
    public void updateTitle(List<String> keyList, Map<String, List<HelpCenterChildEntity>> data) {
        dismissLoadingDialog();
        mAdapter.update(keyList, data);
    }

    @Override
    protected void onDestroy() {

        // webView 内存泄漏
        if (mWebView != null) {
            llContent.removeAllViews();
            mWebView.stopLoading();
            mWebView.getSettings().setJavaScriptEnabled(false);
            mWebView.clearHistory();
            mWebView.clearView();
            mWebView.removeAllViews();
            mWebView.destroy();
        }

        super.onDestroy();
    }

}
